package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.RecordDao;
import com.eric.husky.bean.Record;
import java.util.List;

@Service
public class RecordService {
    @Autowired
	private RecordDao recordDao;

	public Page<Record> list(Record record, Pageable page) {
		Example<Record> example = Example.of(record);
		return recordDao.findAll(example, page);
	}

	public Record get(Integer recordid) {
		return recordDao.findById(recordid).get();
	}

    public List<Record> findList() {
		return recordDao.findAll();
	}

	@Transactional
	public Record save(Record record) {
	    if(record.getRecordid()!=null){
	        Record recordOld = recordDao.findById(record.getRecordid()).orElse(null);
	        if(recordOld!=null){
	            UpdateUtil.copyNullProperties(record,recordOld);
                return recordDao.save(recordOld);
	        }
	    }
	    return recordDao.save(record);
	}

	@Transactional
	public void delete(Integer recordid) {
		recordDao.deleteById(recordid);
	}

}